USE [FootballPoll]
GO

/****** Object:  StoredProcedure [dbo].[GetCurrentVotesByUserID]    Script Date: 12/02/2011 13:27:51 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetCurrentVotesByUserID]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetCurrentVotesByUserID]
GO

USE [FootballPoll]
GO

/****** Object:  StoredProcedure [dbo].[GetCurrentVotesByUserID]    Script Date: 12/02/2011 13:27:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[GetCurrentVotesByUserID] 
    /****** Pass them in as parameters  ******/
   
		@StartDate DATETIME,
		@EndDate DATETIME,
		@UserID Int
AS 
     /****** Explicit declaration for testing  ******/
    /*
		DECLARE @StartDate DATETIME
		DECLARE @EndDate DATETIME
		DECLARE @UserID Int
		SET @StartDate ='2011-10-23'
		SET @EndDate ='2011-10-29'
		SET @UserID = 1
		*/	
/****** Explicit declaration for testing  ******/

	
Select votes.teamID,  teams.teamName, SUM(votes.Points) Points, SUM(votes.FirstPlaceVotes) FP
from votes
Left Outer Join  teams
on votes.teamID = teams.id
Join users
on Votes.UserID = Users.id
Where
		Votes.UserID = @UserID /*here i want to use the username */
and
		(SELECT DATEDIFF(Day, votes.Date , @EndDate)) >= 0
and
		(SELECT DATEDIFF(Day, votes.Date , @StartDate)) <= 0
  GROUP BY teamID, teams.teamName
  
  Order by sum(votes.Points) DESC


GO

